//
//  WYYXLiveView.swift
//  liveplugin
//
//  Created by yunxiwangluo on 2020/4/22.
//

import UIKit
import NMCLiveStreaming


class WYYXLiveView: NSObject,FlutterPlatformView {
    
    var mediaCapture: LSMediaCapture?
    
    var previewView: UIView!
    var registrar: FlutterPluginRegistrar
    var channel: FlutterMethodChannel
    
    var liveManager: WYYXLiveManager?
    
    public init(withFrame frame: CGRect, withRegistrar registrar: FlutterPluginRegistrar, withId id: Int64){
           self.registrar = registrar
           previewView = UIView(frame: frame)
           channel = FlutterMethodChannel(name: "com.sunstar.liveplugin.viewControl", binaryMessenger: registrar.messenger())
        
        liveManager = WYYXLiveManager();
        
//        liveManager?.initMediaCapture(url: "",)
       }
    
    func view() -> UIView {
        
        channel.setMethodCallHandler { [weak self](call: FlutterMethodCall, result: FlutterResult) in
            
            let dict = call.arguments as? Dictionary<String,Any>

            switch call.method{
                
            case "initMediaCapture":
                self?.liveManager?.initMediaCapture(url: "",isVertical: dict?["isVertical"] as! Bool)
                break
            case "preView":
                if let v = self?.previewView{
                    self?.liveManager?.startVideoPreView(preView: v, isOpenFace: dict?["isFaceU"] as! Bool);
                }
                break
            case "changeFilter":
                //self?.liveManager?.changeFilter(argument: dict)
                break
            case "switchCamera":
                self?.liveManager?.switchCamera()
                break
            case "addWaterMarker":
                self?.liveManager?.addWaterMarker()
                break
            case "startPushStreaming":
                self?.liveManager?.startPushStream(call: call)
                break
            case "initBeauty":
                self?.liveManager?.setBeauty(whiteColor: dict?["whiteColor"] as! Double, redColor: dict?["redColor"] as! Double, blur: dict?["blur"] as! Double, filterName: dict?["filterName"] as! String, filterValue: dict?["filterValue"] as! Double)
                break
            case "changeOrigation":
                if let d = dict{
                    self?.liveManager?.changeOrigation(isVertical: d["isVertical"] as! Bool)
                }
            break
            case "destroy":
                self?.liveManager?.destroy()
                break
            case "pauseLiveStream":
                self?.liveManager?.pauseLiveStream()
            break
            case "rePushLiveStream":
                self?.liveManager?.rePushLiveStream()
            break
                
            default:
                result(FlutterMethodNotImplemented)
                break
            }

        }
        
        return previewView
    }
    

}

